/*
 * Copyright (c) 2023-2025 elsfs Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.elsfs.cloud.common.security.customer;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import java.util.Collections;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springframework.stereotype.Component;

/**
 * 登录文档
 *
 * @author zeng
 */
@Slf4j
@Component
public class UsernamePasswordJsonApiPlugin implements GlobalOpenApiCustomizer {

  /**
   * 构建自定义接口
   *
   * @param openApi OpenAPI
   */
  @Override
  public void customise(OpenAPI openApi) {
    LOGGER.info("customer.账号密码json登录接口");
    // 基础信息 构建Operation
    Operation operation = new Operation();
    operation.operationId("UsernamePasswordJsonApi");
    operation.summary("账号密码json登录接口");
    operation.description("根据用户名和密码登录获取token");
    operation.tags(Collections.singletonList("登录"));
    // 构建请求体
    RequestBody requestBody = new RequestBody();
    requestBody.required(true);
    ObjectSchema objectSchema = new ObjectSchema();
    objectSchema.addProperty(
        "username", new StringSchema().type("string").example("admin").description("用户名"));
    objectSchema.addProperty(
        "password", new StringSchema().type("string").example("Elsfs.2023").description("密码"));
    requestBody.content(
        new Content()
            .addMediaType(
                "application/json",
                new MediaType()
                    .example("{\n  \"username\": \"admin\",\n  \"password\": \"Elsfs.2023\"\n}")
                    .schema(objectSchema)));
    operation.requestBody(requestBody);
    // 构建响应body
    ApiResponses apiResponses = new ApiResponses();
    ApiResponse apiResponse = new ApiResponse();
    apiResponse
        .description("ok")
        .content(new Content().addMediaType("*/*", new MediaType().schema(new StringSchema())));
    apiResponses.addApiResponse("200", apiResponse);
    operation.responses(apiResponses);
    // 因为要新增自定义的接口，直接这里add
    PathItem pathItem = new PathItem();
    // 该自定义接口为post
    pathItem.post(operation);
    openApi.path("/login", pathItem);
  }
}
